package com.sr.examination.exam.common;

import android.view.View;

import com.sr.examination.R;
import com.sr.examination.exam.common.widget.PayAlertDialog;
import com.sr.examination.net.Net;
import com.sr.examination.user.Account;
import com.sr.examination.user.wallet.WalletActivity;
import com.tl.commonlibrary.network.ErrorResponse;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.network.bean.base.BaseBean;
import com.tl.commonlibrary.tool.NumberUnit;
import com.tl.commonlibrary.tool.UIUtils;
import com.tl.commonlibrary.ui.BaseFragmentActivity;
import com.tl.commonlibrary.ui.beans.CoinBean;
import com.tl.commonlibrary.ui.beans.User;

import retrofit2.Call;

/**
 * Created by JongSung on 18/3/13.
 */

public class PayModel {
    private BaseFragmentActivity context;
    private long paperId;
    private PayAlertDialog payAlertDialog;
    private ExamPurchaseListener listener;

    public PayModel(BaseFragmentActivity context) {
        this.context = context;
    }

    public long getPaperId() {
        return paperId;
    }

    public void setPaperId(long paperId) {
        this.paperId = paperId;
    }

    public void setListener(ExamPurchaseListener listener) {
        this.listener = listener;
    }

    public void show(String coin) {
        if (Account.isLogged(context)) {
            if (payAlertDialog == null) {
                payAlertDialog = new PayAlertDialog(context);
                payAlertDialog.setListener(new PayAlertDialog.OnAlertClickListener() {
                    @Override
                    public void onRightClicked(View view) {
                        examPurchase();
                    }

                    @Override
                    public void onLeftClicked(View view) {

                    }

                    @Override
                    public void onRecharge(View view) {
                        WalletActivity.start(context);
                    }
                });
            }
            payAlertDialog.setMoney(coin, new NumberUnit(Account.getUser().getGoldCoin()).setFormatDot(false).get2F());
            payAlertDialog.show();
        } else if (payAlertDialog != null) {
            payAlertDialog.dismiss();
        }
    }

    private void examPurchase() {
        context.showProgressDialog("正在购买...", false);
        Net.examPurchase(paperId, new RequestListener<BaseBean<CoinBean>>() {
            @Override
            public void onSuccess(Call<BaseBean<CoinBean>> call, BaseBean<CoinBean> response) {
                if (response.isSuccessful()) {
                    User user = Account.getUser();
                    if (user != null) {
                        user.setGoldCoin(response.data.getGoldCoin());
                        com.tl.commonlibrary.ui.user.Account.setUserNoEvent(user);
                        UIUtils.showToast(context.getString(R.string.exam_purchase_success));
                        if (listener != null) {
                            listener.purchaseSuccess();
                        }
                    }
                }
                context.dismissAll();
            }

            @Override
            public void onFailed(Call<BaseBean<CoinBean>> call, ErrorResponse errorResponse) {
                context.dismissAll();
            }
        });
    }

    public interface ExamPurchaseListener {
        void purchaseSuccess();
    }

    public void onDestroy() {
        if (payAlertDialog != null) {
            payAlertDialog.dismiss();
            listener = null;
            payAlertDialog = null;
        }
    }
}
